﻿using Microsoft.VisualStudio.GraphModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Timtopia.DGMLPosterView
{
    public static class DotGraph
    {
        public static Graph Load(string fileName)
        {
            var graph = new Graph();
            var nodePattern = new Regex(@"(^u\d+)\[.*");
            var edgePattern = new Regex(@"(^u\d+) -> (u\d+)\[.*");
            System.IO.StreamReader file =
               new System.IO.StreamReader(fileName);
            string line;
            while ((line = file.ReadLine()) != null)
            {
                var match = nodePattern.Match(line);
                if (match.Success)
                {
                    string id = match.Groups[1].Value;
                    graph.Nodes.GetOrCreate(id);
                }
                match = edgePattern.Match(line);
                if (match.Success)
                {
                    string source = match.Groups[1].Value;
                    string target = match.Groups[2].Value;
                    graph.Links.GetOrCreate(source, target);
                }
            }
            return graph;
        }
    }
}
